package cn.hdu.liang.ch5;

import java.util.*;
import java.util.stream.Collectors;

public class PuttingIntoPractice {
    public static void main(String[] args) {

        Trader raoul = new Trader("Raoul", "剑桥");
        Trader mario = new Trader("Mario", "米兰");
        Trader alan = new Trader("Alan", "剑桥");
        Trader brian = new Trader("Brian", "剑桥");

        List<Transaction> tas = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        //找出2011年所有交易并按照交易额排序
        List<Transaction> tr2011 = tas.stream()
                .filter(t -> t.getYear() == 2011)
                .sorted(Comparator.comparing(Transaction::getValue))
                .collect(Collectors.toList());
        System.out.println(tr2011);

        //交易员都在那些不同的城市工作过
        Set<String> cities = tas.stream()
                .map(t -> t.getTrader().getCity())
                .collect(Collectors.toSet());
        System.out.println(cities);

        //查找所有来自于剑桥的交易员，并按姓名排序
        List<Transaction> traders = tas.stream()
                .filter(t -> t.getTrader().getCity().equals("剑桥"))
                .distinct()
                .sorted(Comparator.comparing(t -> t.getTrader().getName()))
                .collect(Collectors.toList());
        System.out.println(traders);

        //返回所有交易员的姓名字符串，按字母顺序排序
        String traderStr = tas.stream()
                .map(t -> t.getTrader().getName())
                .distinct()
                .sorted()
                .collect(Collectors.joining());
        System.out.println(traderStr);

        //有没有交易员是在米兰工作的
        boolean milanBased = tas.stream()
                .anyMatch(t -> t.getTrader().getCity().equals("米兰"));
        System.out.println(milanBased);

        //打印生活在剑桥的交易员的所有交易额
        tas.stream()
                .filter(t -> t.getTrader().getCity().equals("剑桥"))
                .map(Transaction::getValue)
                .forEach(System.out::println);

        //所有交易中，最高的交易额是多少
        Optional<Integer> highestValue = tas.stream()
                .map(Transaction::getValue)
                .reduce(Integer::max);
        System.out.println(highestValue);

        //找到交易额最小的交易
        Optional<Transaction> smallestTrans = tas.stream()
                .min(Comparator.comparing(Transaction::getValue));
        System.out.println(smallestTrans);
    }
}
